/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lucene.simple.search;

import java.io.File;
import java.io.IOException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.spell.LuceneDictionary;
import org.apache.lucene.search.spell.SpellChecker;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

/**
 *
 * @author Content Pilot
 */
public class SimpleSuggestionService {
    public static void main(String[] args) throws IOException{
        File checkDir = new File("c:/spellchecker/");
        File indexDir = new File("c:/luence_index/");
        Directory checkDirectory = FSDirectory.open(checkDir);
        
        Directory indexDirectory = FSDirectory.open(indexDir); // or FSDirectory
        IndexReader ir = IndexReader.open(indexDirectory);
        
        SpellChecker spellChecker = new SpellChecker(checkDirectory);
        spellChecker.indexDictionary(new LuceneDictionary(ir, "contents"));
        
        String wordForSuggestion = "wepbortal";
        int suggestionNumber = 5;
        
        String[] suggestions = spellChecker.suggestSimilar(wordForSuggestion, suggestionNumber);
        
        if(suggestions != null && suggestions.length > 0 ){
            for ( String word : suggestions ) {
                System.out.println("Did you mean -> " + word);
            }
        } else {
            System.out.println("No Suggestions found for word: " + wordForSuggestion);
        }
        
    }
}
